package com.delio.client.pagos.empleados;

import java.util.ArrayList;
import java.util.List;

import com.delio.client.idioma.DelioConstants;
import com.delio.client.idioma.DelioMessages;
import com.delio.client.pagos.contratos.Contratos;
import com.delio.client.pagos.contratos.PanelNuevoContrato;
import com.delio.client.util.RemoteStoreFilterField;
import com.delio.client.util.Utilities;
import com.extjs.gxt.ui.client.Style.SelectionMode;
import com.extjs.gxt.ui.client.data.BeanModel;
import com.extjs.gxt.ui.client.data.ModelData;
import com.extjs.gxt.ui.client.event.ButtonEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.GridEvent;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.event.MessageBoxEvent;
import com.extjs.gxt.ui.client.event.SelectionChangedEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.store.Store;
import com.extjs.gxt.ui.client.util.IconHelper;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.Label;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.grid.CheckBoxSelectionModel;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.extjs.gxt.ui.client.widget.grid.Grid;
import com.extjs.gxt.ui.client.widget.grid.filters.GridFilters;
import com.extjs.gxt.ui.client.widget.grid.filters.StringFilter;
import com.extjs.gxt.ui.client.widget.layout.FitLayout;
import com.extjs.gxt.ui.client.widget.toolbar.PagingToolBar;
import com.extjs.gxt.ui.client.widget.toolbar.ToolBar;
import com.google.gwt.core.client.GWT;

public class PanelGridEmpleado extends ContentPanel {
	private DelioConstants k = GWT.create(DelioConstants.class);
	private DelioMessages msg = GWT.create(DelioMessages.class);

	private Button btnDelete;
	private ToolBar toolbar;
	private PagingToolBar toolBarPag;

	private List<ColumnConfig> columns;
	private CheckBoxSelectionModel<BeanModel> sm;
	private ColumnModel cm;
	public static Grid<BeanModel> grid;

	private BeanModel selectedModel;
	private List<BeanModel> selectedModels;
	private Integer selectedSize;

	private Listener<MessageBoxEvent> listener;
	private SelectionListener<ButtonEvent> sl_delete;
	
	private GridFilters filters;
	@SuppressWarnings("unused")
	
	private String text = null;
	private RemoteStoreFilterField<BeanModel> filterField;

	public static PanelNuevoEmpleado panelnuevoempleado;
	public static PanelContratoEmpleado panelcontratoempleado;
	public static PanelNuevoContrato panelnuevocontrato;
	
	@SuppressWarnings({ "rawtypes", "unchecked" })
	public PanelGridEmpleado() {
		super();
		setBodyBorder(false);
		setHeaderVisible(false);
		setIcon(IconHelper.createPath("images/icons/table.png"));
		setHeading(k.ListaEmpleados());
		setLayout(new FitLayout());

	
		Empleados.getPaginate();
		Empleados.loader.load();

		btnDelete = new Button(k.eliminar(),IconHelper.createPath("images/icons/delete.gif"));
		btnDelete.setEnabled(false);
		

		toolbar = new ToolBar();
		toolbar.setStyleAttribute("backgroundImage","url(gxt/images/default/panel/white-top-bottom.gif)");
		toolbar.setBorders(true);
		
		filters = new GridFilters();
		filters.addFilter(new StringFilter("dni"));
		filters.addFilter(new StringFilter("nombres"));
		filters.addFilter(new StringFilter("apellidos"));
		filters.addFilter(new StringFilter("domicilio"));
		filters.addFilter(new StringFilter("telefono"));
		filters.addFilter(new StringFilter("celular"));
		filters.addFilter(new StringFilter("correo"));
		
		filterField = new RemoteStoreFilterField() {

			@Override
			protected void handleOnFilter(String filter) {
				if (filter == null || filter.equals(""))
					text = null;
				else
					text = filter.toLowerCase();
				Empleados.filter=filter;
				Empleados.loader.load();
			}

			@Override
			protected boolean doSelect(Store store, ModelData parent,
					ModelData record, String property, String filter) {
				String name = record.get("nombres");

				name = name.toLowerCase();

				if (name.startsWith(filter.toLowerCase())) {
					return true;
				}
				return false;
			}
		};
		filterField.bind(Empleados.store);
		filterField.setEmptyText(k.buscar());
		filterField.setWidth(250);

		toolbar.add(filterField);
		toolbar.add(btnDelete);
		
		Label label =new Label("Listado de Empleados");
		label.setStyleAttribute("marginLeft", "200px");
		label.setStyleAttribute("fontSize", "13px");
		label.setStyleAttribute("fontWeight", "bold");
		label.addStyleName("x-panel-header-text-default");
		
		toolbar.add(label);


		toolBarPag = new PagingToolBar(Utilities.itemsPerPage);
		toolBarPag.bind(Empleados.loader);
		toolBarPag.setBorders(true);

		columns = new ArrayList<ColumnConfig>();

		sm = new CheckBoxSelectionModel<BeanModel>();
		sm.setSelectionMode(SelectionMode.SINGLE);
		

		columns.add(sm.getColumn());
		columns.add(new ColumnConfig("dni", k.dni(), 70));
		columns.add(new ColumnConfig("nombres", k.Nombres(), 100));
		columns.add(new ColumnConfig("apellidos", k.Apellidos(), 250));
		columns.add(new ColumnConfig("domicilio", k.Domicilio(), 300));
		columns.add(new ColumnConfig("telefono", k.Telefono(), 100));
		columns.add(new ColumnConfig("celular", k.Celular(), 100));
		columns.add(new ColumnConfig("correo", k.Correo(), 200));

		ColumnConfig column = new ColumnConfig("nombrecompleto", "", 0);
		column.setHidden(true);

		columns.add(column);

		cm = new ColumnModel(columns);

		grid = new Grid<BeanModel>(Empleados.store, cm);
		grid.setAutoExpandColumn("apellidos");
		grid.setLoadMask(true);
		grid.setBorders(true);
		grid.addPlugin(sm);
		grid.addPlugin(filters);
		grid.setSelectionModel(sm);
		grid.addListener(Events.Attach, new Listener<GridEvent<BeanModel>>() {
			@Override
			public void handleEvent(GridEvent<BeanModel> be) {
				Empleados.loader.load(0, Utilities.itemsPerPage);
			}
		});

		grid.getSelectionModel().addListener(Events.SelectionChange,
				new Listener<SelectionChangedEvent<BeanModel>>() {

					@Override
					public void handleEvent(SelectionChangedEvent<BeanModel> be) {
						selectedSize = be.getSelection().size();

						if (selectedSize == 0) {
							Contratos.empleado_id=0;
							Contratos.store.removeAll();
							panelnuevoempleado.unbind();
							panelcontratoempleado.nuevoHeader();
							btnDelete.setEnabled(false);
							
							
							
						} else if (selectedSize == 1) {
							selectedModel = be.getSelectedItem();
							panelnuevoempleado.bind(selectedModel);
							
						
							Contratos.empleado_id=selectedModel.get("id");
							Contratos.loader.load();
							panelcontratoempleado.modificarHeader();
							btnDelete.setEnabled(true);
							
						} else if (selectedSize > 1) {
							Contratos.empleado_id=0;
							
							selectedModels = be.getSelection();
							panelnuevoempleado.unbind();
							panelcontratoempleado.nuevoHeader();
							btnDelete.setEnabled(true);
							Contratos.store.removeAll();
						}

					}

				});

		listener = new Listener<MessageBoxEvent>() {

			@Override
			public void handleEvent(MessageBoxEvent be) {
				String button_id = be.getButtonClicked().getItemId();
				if (Dialog.YES == button_id) {
					if (selectedSize == 1) {
						Integer id = selectedModel.get("id");
						Empleados.delete(id);
					} else if (selectedSize > 1) {
						String ids = Utilities.toIds(selectedModels);
						Empleados.deleteAll(ids);
					}
				}
			}
		};
		sl_delete = new SelectionListener<ButtonEvent>() {

			@Override
			public void componentSelected(ButtonEvent ce) {
				String mensaje = "";
				if (selectedSize == 1) {
					String nombres = selectedModel.get("nombrecompleto");
					mensaje = msg.empleadoEstaSeguro(nombres);

				} else if (selectedSize > 1) {
					mensaje = k.eliminarAll();
				}
				MessageBox.confirm(k.confirmacion(), mensaje, listener)
						.getDialog().getButtonById(Dialog.YES).setText(k.si());

			}
		};

		btnDelete.addSelectionListener(sl_delete);

		setTopComponent(toolbar);
		setBottomComponent(toolBarPag);

		add(grid);

	}
}


//
//filter = new StoreFilterField<BeanModel>() {
//
//	@Override
//	protected boolean doSelect(Store<BeanModel> store,
//			BeanModel parent, BeanModel record, String property,
//			String filter) {
//
//		String name = record.get("nombres");
//
//		name = name.toLowerCase();
//
//		if (name.startsWith(filter.toLowerCase())) {
//			return true;
//		}
//		return false;
//	}
//
//
//
//};
//// filter.bind(Empleados.store);